package cn.tedu._07vehicle.controller;

import cn.tedu._07vehicle.base.response.JsonResult;
import cn.tedu._07vehicle.base.response.StatusCode;
import cn.tedu._07vehicle.mapper.VehicleMapper;
import cn.tedu._07vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._07vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._07vehicle.pojo.entity.Vehicle;
import cn.tedu._07vehicle.pojo.vo.UserVo;
import cn.tedu._07vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/v1/vehicle/")
@Tag(name = "车辆管理模块")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("add")
    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order =10)
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, HttpSession session) {
        log.debug("添加车辆参数"+vehicleAddParam);
        UserVo userVo =(UserVo) session.getAttribute("user");

        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setUserId(userVo.getId());
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicleMapper.insertVehicle(vehicle);
        return JsonResult.ok();
    }

    /**
     *查询车辆列表功能
     */
    @GetMapping("list")
    @Operation(summary = "查询车辆列表")
    @ApiOperationSupport(order = 20)
    @Parameters(value = {
            @Parameter(name = "brand",description = "车辆品牌",required = true),
            @Parameter(name = "license",description="车牌号",required = true),
            @Parameter(name = "vehicleListQuery",hidden = true)
    })
    public JsonResult listVehicle( VehicleListQuery vehicleListQuery,HttpSession session){
        log.debug("查询车辆列表参数:"+vehicleListQuery);
        //校验用户的登录状态;
        UserVo userVo =(UserVo) session.getAttribute("user");

        //登陆成功
        Long userId = userVo.getId();
        List<VehicleListVO> vehicleListVO=vehicleMapper.selectVehicleList(vehicleListQuery,userId);
        return JsonResult.ok(vehicleListVO);
    }
    //删除车辆功能
    @PostMapping("del/{id}")
    @Operation(summary = "删除车辆功能")
    @ApiOperationSupport(order = 30)
    public JsonResult deleteVehicle(@PathVariable @Range(min = 1,max = 500,message = "车辆id必须在1-500之间") Long id, HttpSession session){
        //手动抛出异常
        if(id>0){
            throw new IllegalArgumentException("车辆id不能大于0");
        }
        log.debug("查询删除状态"+id);
        UserVo userVo =(UserVo) session.getAttribute("user");

        int rows= vehicleMapper.selectVehicleById(id,userVo.getId());

        return JsonResult.ok();
    }
}
